package com.sychip.fhc.app.accontrol


import android.content.Context
import android.widget.Toast
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.toSize
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.fhc.connectivity.ConnectionState
import com.fhc.connectivity.NetworkViewModel
import com.fhc.view.utils.ToastyShow
import com.sychip.fhc.app.pref.ACMode
import com.sychip.fhc.app.pref.ACStatus
import com.sychip.fhc.app.todo.R
import com.sychip.fhc.app.todo.StartViewModel


@Preview
@Composable
private fun ACImage(acStatus:ACStatus = ACStatus(powerOn = true)){
    val bitmap = ImageBitmap.imageResource(id = R.drawable.ac2)
    val renderHeight = bitmap.height * 1.0f
    val renderWidth = bitmap.width  * 1.0f
    val modifier = Modifier.fillMaxWidth().padding(80.dp).padding(top = 80.dp).aspectRatio(renderWidth / renderHeight, matchHeightConstraintsFirst = true)

    var targetRadius by remember { mutableStateOf(28f) }
    val circleSize = remember { Animatable(20f) }
//    val color = Color.Red
//    val stroke = Stroke(width = 5f)

    LaunchedEffect(key1 = true) {
        circleSize.animateTo(targetValue = targetRadius, animationSpec = infiniteRepeatable(tween(600), RepeatMode.Reverse))
    }

    var boxSize by remember { mutableStateOf(Size.Zero)}
    Box(modifier = modifier.fillMaxWidth().onGloballyPositioned{it->
        boxSize = it.size.toSize()
    }){
        Image(
            bitmap = bitmap,
            colorFilter = ColorFilter.tint(Color.White.copy(alpha = 0.7f)),
            contentDescription = "Zoomable image",
            contentScale = ContentScale.Fit,
            modifier = Modifier.fillMaxSize()
        )
        if(acStatus.powerOn) {
            Canvas(modifier = Modifier.fillMaxSize().graphicsLayer()) {
                val x = (boxSize.width * (800.0 / 860.0)).toFloat()
                val y = (boxSize.height * (234.0 / 564.0)).toFloat()
                val centerOffset = Offset(x, y)
                drawCircle(
                    color = Color.White,
                    radius = circleSize.value,
                    center = centerOffset,
                )
                drawCircle(
                    color = Color.Blue.copy(alpha = .6f),
                    radius = 20f,
                    center = centerOffset,
                )

            }
        }
    }
}

@Preview
@Composable
fun AcControl(viewModel: StartViewModel = hiltViewModel(), nwVewModel: NetworkViewModel = hiltViewModel()){

//    val segment7Font = FontFamily(
//        Font(R.font.segment7, FontWeight.Light),
//        Font(R.font.segment7, FontWeight.Normal),
//        Font(R.font.segment7, FontWeight.Normal, FontStyle.Italic),
//        Font(R.font.segment7, FontWeight.Medium),
//        Font(R.font.segment7, FontWeight.Bold)
//    )
    val ctx: Context = LocalContext.current
    val aa by nwVewModel.networkState.collectAsStateWithLifecycle()
    if (aa == ConnectionState.Unavailable) {
        Toast.makeText(ctx, "ConnectionState.Unavailable", Toast.LENGTH_LONG).show()
    }
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    if(uiState.toastMsgId != null){
        ToastyShow(msgRes = uiState.toastMsgId!!,uiState.toastMsgArgs, showAfter = {
            viewModel.toastMessageShown()
        })
    }
    val acStatus by viewModel.acStatus.collectAsStateWithLifecycle()
    val colorOn = colorScheme.primary
    val colorOff = Color.DarkGray.copy(alpha = 0.6f)
    val modeFont = 22.sp
    Column(modifier = Modifier.fillMaxSize().background(colorScheme.primary),){
        ACImage(acStatus)
        Column(modifier = Modifier.fillMaxSize().background(Color.White),) {
            Row(modifier = Modifier.fillMaxWidth().padding(top=20.dp).height(100.dp)) {
//                Card(
//                    modifier = Modifier.size(100.dp).padding(10.dp),
//                    shape = RoundedCornerShape(16.dp),
//                    border  = if(acStatus.isMode(ACMode.AUTO))  BorderStroke(width = 2.dp, brush = SolidColor(colorOn)) else null,
//                ) {
////                    Image(
////                        painter = painterResource(id = R.drawable.se_auto),
////                        colorFilter = ColorFilter.tint(Color.White),
////                        contentDescription = null,
////                        modifier = Modifier.fillMaxSize().padding(10.dp)
////                    )
//
//                    Box(modifier = Modifier.fillMaxSize()){
//                        Text("AUTO",
//                            color =  if(acStatus.isMode(ACMode.AUTO))  colorOn else colorOff,
//                            modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
//                            fontSize = modeFont,
//                            fontWeight = FontWeight.Bold)
//                    }
//                }
                Spacer(modifier = Modifier.weight(1f))
                Row(modifier = Modifier.wrapContentSize(align = Alignment.Center)) {
                    if(acStatus.powerOn){
                    Image(
                        painter = painterResource(id = R.drawable.ac_temp),
                        colorFilter = ColorFilter.tint(colorOn),
                        contentDescription = null,
                        modifier = Modifier.wrapContentSize(align = Alignment.Center),
                    )
                    Text(modifier = Modifier.fillMaxHeight().wrapContentSize(align = Alignment.Center).padding(start = 10.dp).offset(y = (0).dp),
                        text = "${acStatus.temp}°C" ,
                        color = colorOn,
                        fontFamily = FontFamily.SansSerif,
                        fontSize = 48.sp,
                        fontWeight = FontWeight.Bold)
                        }
//                    Row(modifier = Modifier.wrapContentSize().offset(x= (-10).dp,y = (-0).dp).align(Alignment.Center)) {
//
//                        Image(
//                            painter = painterResource(id = R.drawable.se_ac_fan),
//                            colorFilter = ColorFilter.tint(Color.Black),
//                            contentDescription = null,
//                            modifier = Modifier.size(40.dp).padding(10.dp).offset(y = (-30).dp)
//                        )
//                        Image(
//                            painter = painterResource(id = R.drawable.se_ac_fan),
//                            colorFilter = ColorFilter.tint(Color.LightGray),
//                            contentDescription = null,
//                            modifier = Modifier.size(40.dp).padding(5.dp).offset(y = (-30).dp)
//                        )
//                        Image(
//                            painter = painterResource(id = R.drawable.se_ac_fan),
//                            colorFilter = ColorFilter.tint(Color.LightGray),
//                            contentDescription = null,
//                            modifier = Modifier.size(40.dp).offset(x = 10.dp,y = (-30).dp)
//                        )
//                        Image(
//                            painter = painterResource(id = R.drawable.se_auto),
//                            colorFilter = ColorFilter.tint(Color.LightGray),
//                            contentDescription = null,
//                            modifier = Modifier.size(40.dp).offset(x = 10.dp,y = (-30).dp)
//                        )
//                    }
                }
                Spacer(modifier = Modifier.weight(1f))
//                Card(
//                    modifier = Modifier.size(100.dp).padding(10.dp),
//                    shape = RoundedCornerShape(16.dp),
//                    border  = if(acStatus.isMode(ACMode.AUTO))  BorderStroke(width = 2.dp, brush = SolidColor(colorOn)) else null,
//
//                    ) {
//                    Image(
//                        painter = painterResource(id = R.drawable.se_power_off),
//                        colorFilter = if(acStatus.powerOn) ColorFilter.tint(colorOn) else ColorFilter.tint(colorOff),
//                        contentDescription = null,
//                        modifier = Modifier.fillMaxSize().padding(16.dp)
//                    )
//                }
            }
            Spacer(modifier = Modifier.weight(1f))
            Row(modifier = Modifier.fillMaxWidth().padding(start = 20.dp, end = 20.dp).height(100.dp)) {
                Card(
                    modifier = Modifier.size(100.dp).padding(10.dp),
                    shape = RoundedCornerShape(16.dp),
                    border  = if(acStatus.isMode(ACMode.AUTO))  BorderStroke(width = 2.dp, brush = SolidColor(colorOn)) else null,
                ) {

                    Image(
                        painter = painterResource(id = R.drawable.ac_auto),
                        colorFilter = ColorFilter.tint(if(acStatus.isMode(ACMode.AUTO)) colorOn else colorOff),
                        contentDescription = null,
                        modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
                    )
//                    Box(modifier = Modifier.fillMaxSize()){
//                        Text("AUTO",
//                            color =  if(acStatus.isMode(ACMode.AUTO))  colorOn else colorOff,
//                            modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
//                            fontSize = modeFont,
//                            fontWeight = FontWeight.Bold)
//                    }
                }
                Spacer(modifier = Modifier.weight(1f))
                Card(
                    modifier = Modifier.size(100.dp).padding(10.dp),
                    shape = RoundedCornerShape(16.dp),
                    border  = if(acStatus.isMode(ACMode.COOL))  BorderStroke(width = 2.dp, brush = SolidColor(colorOn)) else null,
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.ac_cooling),
                        colorFilter = ColorFilter.tint(if(acStatus.isMode(ACMode.COOL)) colorOn else colorOff),
                        contentDescription = null,
                        modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
                    )
//                    Box(modifier = Modifier.fillMaxSize()){
//                        Text("COOL",
//                            color =  if(acStatus.isMode(ACMode.COOL)) colorOn else colorOff,
//                            modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
//                            fontSize = modeFont,
//                            fontWeight = FontWeight.Bold)
//                    }
                }
                Spacer(modifier = Modifier.weight(1f))
                Card(
                    modifier = Modifier.size(100.dp).padding(10.dp),
                    shape = RoundedCornerShape(16.dp),
                    border  = if(acStatus.isMode(ACMode.HEAT))  BorderStroke(width = 2.dp, brush = SolidColor(colorOn)) else null,
                ) {

                    Image(
                        painter = painterResource(id = R.drawable.ac_heating),
                        colorFilter = ColorFilter.tint(if(acStatus.isMode(ACMode.HEAT)) colorOn else colorOff),
                        contentDescription = null,
                        modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
                    )
                    Box(modifier = Modifier.fillMaxSize()){
                        Text("HEAT",
                            color = if(acStatus.isMode(ACMode.HEAT)) colorOn else colorOff,
                            modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
                            fontSize = modeFont,
                            fontWeight = FontWeight.Bold)
                    }
                }
//                Card(
//                    modifier = Modifier.size(100.dp).padding(10.dp),
//                    shape = RoundedCornerShape(16.dp),
//                    border  = if(acStatus.isMode(ACMode.FAN))  BorderStroke(width = 2.dp, brush = SolidColor(colorOn)) else null,
//                ) {
//
////                    Image(
////                        painter = painterResource(id = R.drawable.se_ac_fan),
////                        colorFilter = ColorFilter.tint(Color.White.copy(alpha = 0.6f)),
////                        contentDescription = null,
////                        modifier = Modifier.fillMaxSize().padding(14.dp)
////                    )
//                    Box(modifier = Modifier.fillMaxSize()){
//                        Text("FAN",
//                            color = if(acStatus.isMode(ACMode.FAN)) colorOn else colorOff,
//                            modifier = Modifier.fillMaxSize().wrapContentSize(align = Alignment.Center),
//                            fontSize = modeFont,
//                            fontWeight = FontWeight.Bold)
//                    }
//                }
            }
            Spacer(modifier = Modifier.weight(1f))
            Spacer(modifier = Modifier.weight(1f))
            AppVersion()
        }
    }
}


// 获取应用版本信息
@Composable
fun AppVersion() {
    val context = LocalContext.current
    val appVersion = remember {
        context.packageManager.getPackageInfo(context.packageName, 0).versionName
    }
    val appCode = remember {
        context.packageManager.getPackageInfo(context.packageName, 0).longVersionCode
    }
    Box(modifier = Modifier.fillMaxWidth().padding(bottom = 14.dp)) { // 使用Box组件来填充最大空间
        Text(
            style = TextStyle(
                lineHeight = 16.sp,
                textAlign = TextAlign.Justify
            ),
            maxLines = 3,
            textAlign = TextAlign.Center,
            text = "Copyright © Murata Manufacturing Co., Ltd. \nAll Rights Reserved.\nVersion:$appVersion(build:$appCode)",
            color = Color.DarkGray, // 文本颜色
            fontSize = 12.sp, // 文本大小
            modifier = Modifier
                .align(Alignment.Center) // 设置文本居中对齐
        )
    }
}